package com.laozhao.msas.util;

import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JOptionPane;

/**
 * @author dozhao
 * @version create time：Aug 2, 2011 9:41:43 AM
 * 
 */
public class ToolsUtil {

	// /**
	// * 设置全局透明度
	// */
	// public static Float f_diaphanous = new Float(0.9);

	public static String dateToString(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

//	public static void main(String[] args) {
		// ClassLoader.
		// getC
		// URL resource = ClassLoader.getResource(Constant.DATA_FILE_PATH);
//	}

	public static boolean mustInput(Component owner,String filed, Object value) {
		if (null == value || "".equals(value.toString())) {
			String message = Messages.getString(Constant.CONFIG_ERROR_MUSTINPUT,filed);
			JOptionPane.showMessageDialog(owner,message,
					Messages.getString(Constant.CONFIG_ERROR_TITLE),
					JOptionPane.ERROR_MESSAGE);
			return false;
		} else {
			return true;
		}
	}
	

	public static boolean mustInputNumber(Component owner,String filed, String value) {
		if (mustInput(owner,filed,value)) {
			if (!isNumber(value,false)) {
				String message = Messages.getString(Constant.CONFIG_ERROR_MUSTINPUT,filed);
				JOptionPane.showMessageDialog(owner,message,
						Messages.getString(Constant.CONFIG_ERROR_TITLE),
						JOptionPane.ERROR_MESSAGE);
				return false;
			}else {
				return true;
			}
		} else {
			return false;
		}
	}
	
	public static boolean isNumber(String value,boolean isDecimal){
		if (value == null || "".equals(value)) {
			return false;
		}
		Set<String> set = new HashSet<String>(10);
		for (int i = 0; i < 10; i++) {
			set.add(String.valueOf(i));
		}
		if (isDecimal) {
			set.add(".");
		}
		
		String x;
		for (int i = 0; i < value.length(); i++) {
			x = String.valueOf(value.charAt(i)); 
			if (!set.contains(x)) {
				return false;
			}			
		}
		return true;
	}

//	public static void showMessageDialog(Component owner, String messageCode) {
//		JOptionPane.showMessageDialog(owner, Messages.getString(messageCode),
//				Messages.getString(Constant.CONFIG_ERROR_TITLE),
//				JOptionPane.ERROR_MESSAGE);
//	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 采取add （）方法
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(2011, Calendar.JULY, 6);
		System.out.println( dateToString(calendar1.getTime())); 
		calendar1.add(Calendar.MONTH, 13);
		System.out.println("采取add （）方法后，增加13个月后结果为" + dateToString(calendar1.getTime())); 

		// 采取roll（）方法
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(2011, Calendar.JULY, 6);
		calendar2.roll(Calendar.MONTH, 13);
		System.out.println("采取roll（）方法后，增加13个月后结果为" + dateToString(calendar2.getTime()));

	}

}
